#include "qtbox2dtest.h"
#include "QtBox2DView.h"
#include "QtBox2DRectItem.h"
#include "QtBox2DScene.h"
#include "Box2D/Box2D.h"

#include <QtGui/QApplication>
#include <QtGui/QGraphicsScene>
#include <QTextCodec>


int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	//QApplication::setStyle("plastique");

	QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath()+"/plugins");

	QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
    b2Vec2 gravity(0.0f, -10.0f);

    // Do we want to let bodies sleep?
    bool doSleep = false;

    // Construct a world object, which will hold and simulate the rigid bodies.
    b2World world(gravity, doSleep);

	QtBox2DTest w;
	QtBox2DScene scene(&w);
	scene.initBox2DWorld(&world);
    QtBox2DView view(&scene);
	w.setCentralWidget(&view);


	QObject::connect(&w, SIGNAL(newFile()), &scene, SLOT(newFile()));
	QObject::connect(&w, SIGNAL(save()), &scene, SLOT(save()));
	QObject::connect(&w, SIGNAL(load()), &scene, SLOT(load()));
	QObject::connect(&w, SIGNAL(saveAs()), &scene, SLOT(saveAs()));

	QObject::connect(&w, SIGNAL(undo()), &scene, SLOT(undo()));
	QObject::connect(&w, SIGNAL(redo()), &scene, SLOT(redo()));

	QObject::connect(&scene, SIGNAL(enableUndo(bool)), &w, SIGNAL(enableUndo(bool)));
	QObject::connect(&scene, SIGNAL(enableRedo(bool)), &w, SIGNAL(enableRedo(bool)));

	QObject::connect(&w, SIGNAL(copy()), &scene, SLOT(copy()));
	QObject::connect(&w, SIGNAL(cut()), &scene, SLOT(cut()));
	QObject::connect(&w, SIGNAL(paste()), &scene, SLOT(paste()));

	QObject::connect(&w, SIGNAL(deleteItem()), &scene, SLOT(deleteItem()));
	QObject::connect(&w, SIGNAL(showImageDlg()), &view, SLOT(showImageDlg()));


	QObject::connect(&w, SIGNAL(start(bool)), &scene, SLOT(startSlot(bool)));
	QObject::connect(&w, SIGNAL(createTriggered(QAction *)), &scene, SLOT(createItem(QAction *)));

	QObject::connect(&scene, SIGNAL(changeProperties(const QList<QWidget*>&)), &w, SLOT(changeProperties(const QList<QWidget*>&)));
	scene.setSceneRect(view.rect());
	w.show();
	return a.exec();
}
